export default {
  data() {
    return {
      clickId: '',
      clickToId: '',
      currentNum: 0,
      topList: [],
      isLeftClick: false,
      list: [
        {
          'name': '',
          'foods': [
            {
              'name': '',
              'iconimg': '',
              'id': 1
            },
          ]
        },
      ],
      typeStatus: 'add', // add为发布，edit为编辑
    }
  },

  computed: {
    carInfo() {
      return this.$store.state.afterSales.carInfo
    }
  },

  onLoad(option) {
    console.warn('进入onLoad')
  },

  created() {
    uni.showLoading()
    this.getCategoryList()
  },

  methods: {
    // 获取分类列表
    async getCategoryList() {
      try {
        const res = await this.$api.jsonData.GetCarGoodsTypeAndGoodsList()
        console.log(res.data.data)
        this.handleAddAmount(res.data.data)
        this.list = res.data.data
        this.$nextTick(() => {
          this.getNodesInfo()
        })
      } catch (err) {
        //
      }
      uni.hideLoading()
    },

    // 递归添加数量字段
    handleAddAmount(list) {
      list.forEach(item => {
        if (item.has_Children) {
          this.handleAddAmount(item.carGoodsTypeAndGoodsResponseChildList)
        } else {
          item.maintenanceGoodsLists.forEach(subItem => {
            subItem.amount = 1
          })
        }
      })
    },

    //点击左菜单
    setId(id) {
      console.log(id)
      this.clickId = 'po' + id
      this.isLeftClick = true
      this.currentNum = id
    },
    scroll(e) {
      if (this.isLeftClick) {
        this.isLeftClick = false
        return
      }
      let scrollTop = e.target.scrollTop
      for (let i = 0; i < this.topList.length; i++) {
        let h1 = this.topList[i]
        let h2 = this.topList[i + 1]
        if (scrollTop >= h1 && scrollTop < h2) {
          this.currentNum = i
          this.clickToId = 'to' + i
        }
        //解决滚动到最后选项左侧不会选中
        let length = this.topList.length
        if (scrollTop >= this.topList[length - 1]) {
          this.currentNum = length - 1
          this.clickToId = 'to' + length - 1
        }
      }
    },
    getNodesInfo() {
      //获取节点为.right_title距离顶部的距离，返回值放在数组中
      const query = uni.createSelectorQuery().in(this)
      query
        .selectAll('.class-item')
        .boundingClientRect()
        .exec(res => {
          console.log(res)
          let nodes = res[0]
          let rel = []
          nodes.map(item => {
            rel.push(item.top)
          })
          console.log('节点打印', rel)
          this.topList = rel
        })
    },
    //跳转发布
    bussclick(pData, data) {
      console.log(pData, data)
      this.$store.commit('afterSales/setCategoryGoodsData', data)
      uni.navigateTo({
        url: '/pages_afterSales/selectCategoryDetail/selectCategoryDetail'
      })
    }
  }
}
